<?php
/**
 * Created by JetBrains PhpStorm.
 * User: FUHU
 * Date: 9/28/13
 * Time: 10:05 AM
 * To change this template use File | Settings | File Templates.
 */

namespace Helloworld\Controller;

use Helloworld\Form\UserForm;
use Helloworld\Model\UsersModel;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class HelloworldController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel(array(
            'greeting'=>'helloworld from HelloController',
            'all_users' => $this->getUsersTable()->fetchAll(),
        ));
    }

    public function deleteAction()
    {
        $id = (int)$this->params()->fromRoute('id', 0);
        if($id)
        {
            $request = $this->getRequest();
            if(!$request->isPost())
            {
                return array(
                    'id' => $id,
                    'user_delete' => $this->getUsersTable()->getUser($id)
                );
            }
            else
            {
                $del = $request->getPost('del', 'No');

                if($del == 'Yes')
                {
                    $id = (int)$request->getPost('id');
                    $this->getUsersTable()->deleteUser($id);
               }
            }

            return $this->redirect()->toRoute('hello');
        }
    }

    public function getUsersTable()
    {
        return $this->getServiceLocator()->get('Helloworld\Model\UsersTable');
    }

    public function addAction()
    {
        $form = new UserForm();
        $form->get('submit')->setValue('Submit');

        $request = $this->getRequest();
        if($request->isPost())
        {
            $users = new UsersModel();
            $form->setInputFilter($users->getInputFilter());
            $form->setData($request->getPost());

            if($form->isValid())
            {
                $users->exchangeArray($form->getData());
                $this->getUsersTable()->saveUser($users);

                return $this->redirect()->toRoute('hello');
            }
        }
        return array('form' => $form);
    }

    public function editAction()
    {
        $request = $this->getRequest();

        $sIdSource = ($request->isPost()) ? 'fromPost' : 'fromRoute';
        $id = $this->params()->$sIdSource('id', 0);

        $users = $this->getUsersTable()->getUser($id);

        $form = new UserForm();
        $form->bind($users);

        if($request->isPost())
        {
            $form->setInputFilter($users->getInputFilter());
            $form->setData(array(
                'id' => $id,
                'username' => $this->params()->fromPost('username'),
                'password' => $this->params()->fromPost('password'),
            ));

            echo $id. ' + '.$form->username. ' + '.$form->password;

            if($form->isValid())
            {
                $this->getUsersTable()->saveUser($users);
            }
        }
        else
        {
            $form->get('submit')->setAttribute('value', 'Edit');
            return array(
                'id' => $id,
                'form'=>$form,
            );
        }
        return $this->redirect()->toRoute('hello');
    }


}